<!-- 部门管理 部门列表组件 -->
<template>
  <div class="">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <!-- <el-breadcrumb-item><a>用户管理</a></el-breadcrumb-item> -->
      <el-breadcrumb-item>定时任务</el-breadcrumb-item>
      <el-breadcrumb-item>日志</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <el-form :model="queryInfo" ref="queryForm" size="small" :inline="true" label-width="68px">
        <el-form-item label="任务名称" prop="jobName">
          <el-input v-model="queryInfo.jobName" placeholder="请输入任务名称" clearable />
        </el-form-item>
        <el-form-item label="执行状态" prop="status">
          <el-select v-model="queryInfo.status" placeholder="用户状态" clearable style="width: 240px">
                <el-option label="正常" value="0">正常</el-option>
                <el-option label="暂停" value="1">暂停</el-option>
              </el-select>
        </el-form-item>
        <el-form-item label="任务组名" prop="jobGroup">
          <el-input v-model="queryInfo.jobGroup" placeholder="请输入任务组名" clearable />
        </el-form-item>
        <el-form-item label="执行时间" prop="dateData">
          <el-date-picker v-model="dateData" style="width: 240px" value-format="yyyy-MM-dd hh:mm:ss" type="daterange"
            range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery" type="primary" icon="el-icon-search" size="mini">搜索</el-button>
          <el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8" style="marginBottom:20px">
        <!-- <el-col :span="1.5">
          <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
            @click="deleteTaskHandle">删除</el-button>
        </el-col> -->
        <el-col :span="1.5">
          <el-button type="warning" plain icon="el-icon-delete" size="mini" @click="closeViewHandler">返回</el-button>
        </el-col>
        <!-- right-toolbar  百度:显示/隐藏列操作按钮-->
        <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
      </el-row>
      <el-table ref="multipleTable" :data="userList" tooltip-effect="dark" style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="jobLogId" label="日志编号">
        </el-table-column>
        <el-table-column prop="jobName" label="任务名称">
        </el-table-column>
        <el-table-column prop="jobGroup" label="任务组名">
        </el-table-column>
        <el-table-column prop="invokeTarget" label="调用目标字符串">
        </el-table-column>
        <el-table-column prop="jobMessage" label="日志信息">
        </el-table-column>
        <el-table-column prop="status" label="执行状态">
          <template slot-scope="scope">
            <span :value="scope.row.status">{{ scope.row.status === '0' ? '正常' : '失败' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="执行时间">
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <!-- 添加修改用户 弹窗 -->
    <el-dialog :title="upload.title" width="800px" :visible.sync="dialogVisible" @close="addDialogClosed">
      <!-- 内容主题区域 -->
      <el-form ref="addFormRef" :model="addForm" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务名称" prop="jobName">
              <el-input v-model="addForm.jobName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务分组" prop="jobGroup">
              <el-input v-model="addForm.jobGroup"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="调用方法" prop="invokeTarget"> 
              <span slot="label">
                调用方法1
                <el-tooltip placement="top">
                  <div slot="content">
                    Bean调用示例：ryTask.ryParams('ry')
                    <br />Class类调用示例：com.ruoyi.quartz.task.RyTask.ryParams('ry')
                    <br />参数说明：支持字符串，布尔类型，长整型，浮点型，整型
                  </div>
                  <i class="el-icon-question"></i>
                </el-tooltip>
              </span>
              <el-input v-model="addForm.invokeTarget"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="cron表达式" prop="cronExpression">
              <el-input v-model="addForm.cronExpression"></el-input>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="24">
            <el-form-item label="执行策略" prop="mobile">
              <el-input v-model="addForm.mobile"></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="是否并发" prop="concurrent">
              <el-radio-group v-model="radios">
                <el-radio :label="0">允许</el-radio>
                <el-radio :label="1">禁止</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="部门状态" prop="status">
              <el-radio-group v-model="radio">
                <el-radio :label="0">正常</el-radio>
                <el-radio :label="1">暂停</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  // 组件名称
  name: '',
  // 局部注册的组件
  components: {},
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data() {
    // 验证邮箱的规则 自定义校验规则
    var checkEmail = (rule, value, cb) => {
      // 验证邮箱的正则表达式
      const regEmail = /^([a-zA-Z0-9])+@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-])+/
      if (regEmail.test(value)) {
        // 合法的邮箱
        return cb()
      }
      cb(new Error('请输入合法的邮箱!'))
    }
    // 验证手机的规则
    var checkMobile = (rule, value, cb) => {
      // 验证手机号的规则
      const regMobile = /^(0|86|17951)?(13[0-9]|15[0123456789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
      if (regMobile.test(value)) {
        return cb()
      }
      cb(new Error('请输入合法的手机号!'))

    }
    return {
      radio: 1,
      radios: 1,
      dateRange: [new Date(2000, 10, 10, 10, 10), new Date(2000, 10, 11, 10, 10)], // 日期
      // 多选选中的数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      form: {},
      // showSearch: true,
      detail: {}, // 任务详情
      dateData: [],
      // 获取用户列表数据的参数对象
      queryInfo: {
        // 当前的页数
        currentPage: 1,
        // 当前每页显示多少条数据
        pageSize: 10,
        jobName: undefined,
        jobGroup: undefined,
        status: undefined,
      },
      userList: [],
      total: 0,
      // 对话框显示与隐藏
      dialogVisible: false,
      // 是否显示详细弹出层
      openView: false,
      // 表单参数
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // // 设置上传的请求头部
        // headers: { Authorization: 'Bearer ' + getToken() },
        // // 上传的地址
        // url: process.env.VUE_APP_BASE_API + '/system/user/importData'
      },
      // 添加用户的表单数据
      addForm: {},
      // 添加表单验证的校验规则对象
      // addFormRules: {
      //   jobName: [
      //     { required: true, message: '任务名称不能为空', trigger: 'blur' }
      //   ],
      //   invokeTarget: [
      //     { required: true, message: '调用目标字符串不能为空', trigger: 'blur' }
      //   ],
      //   cronExpression: [
      //     { required: true, message: 'cron执行表达式不能为空', trigger: 'blur' }
      //   ]
      // },
    }
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  created() {
    this.getUserList()
  },
  methods: {
    // 日志列表
    async getUserList() {
      const a = this.$route.query.row
      console.log(a);
      this.queryInfo.jobGroup = this.$route.query.row.jobGroup //任务组名
      this.queryInfo.jobName = this.$route.query.row.jobName // renwu mingcheng
      const params = this.queryInfo
      this.queryInfo.createTimeOpen = this.dateData[0]
      this.queryInfo.createTimeClose = this.dateData[1]
      const { data: res } = await this.$http.post('/sys-job/logPage', params)
      if (res.code === 20000) {
        this.userList = res.data.records
        this.total = parseInt(res.data.total)
      } else {
        this.$message.error(res.msg)
      }
    },
    // 监听 switch 开关的状态
    async userStateChanged(userInfo) {
      console.log(userInfo);
      const { data: res } = await this.$http.put(`user/${userInfo.id}/state/${userInfo.state}`)
      if (res.code === 200) {
        userInfo.state = !userInfo.state
        return this.$message.error('更新用户状态失败!')
      }
      this.$meassage.success('更新用户状态成功!')
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryInfo.currentPage = 1
      this.getUserList()
    },
    // 重置按钮操作
    resetQuery() {
      // this.resetForm('queryForm')
      // console.log(this) this指向$refs的是通过ref获得的实例对象loginFormRef
      this.dateData = []
      this.$refs.queryForm.resetFields()
      this.handleQuery()
    },
    // 监听 pageSize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize);
      this.queryInfo.pageSize = newSize;
      this.getUserList()
    },
    // 监听页码值改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage);
      this.queryInfo.currentPage = newPage
      this.getUserList()
    },
    // 监听添加用户对话框关闭事件
    addDialogClosed() {
      // 重置表单
      this.$refs['addFormRef'].resetFields()
    },
    // 新增按钮
    addTaskHandle() {
      this.dialogVisible = true
      this.upload.title = "添加任务"
      console.log(1);
    },
    // 修改按钮
    updateTaskHandle(row) {
      this.dialogVisible = true
      this.upload.title = "修改任务"
      this.addForm = row
      console.log(row);
    },
    // 多选按钮 修改
    async updateTasksHandle() {
      this.dialogVisible = true
      this.upload.title = "修改任务"
      this.addForm = this.rowData[0]
    },
    // 提交按钮 添加修改任务
    addUser() {
      this.$refs['addFormRef'].validate(async valid => {
        if (valid) {
          console.log(this.addForm);
          if (this.addForm.jobId !== undefined) {
            console.log(this.addForm.jobId, '1111');
            const params = this.addForm
            const { data: res } = await this.$http.post('/sys-job/updateTask', params)
            console.log(res);
            if (res.code === 20000) {
              this.$message.success(`修改成功!`)
              this.getUserList()
              this.dialogVisible = false
            } else {
              this.$message.error(res.msg)
            }
          } else {
            console.log('id');
            const params = this.addForm
            const { data: res } = await this.$http.post('/sys-job/addTask', params)
            console.log(res);
            if (res.code === 20000) {
              this.$message.success(`新增成功!`)
              this.getUserList()
              this.dialogVisible = false
            } else {
              this.$message.error(res.msg)
            }
          }
        }
      })
    },
    // 批量删除
    async deleteTaskHandle() {
      const jobLogIds = this.ids
      // let a = [] // 循环取往数组添加值
      // for (var val in jobLogIds) {
      //   console.log(jobLogIds[val]);
      //   a.push(jobLogIds[val])
      // }
      const { data: res } = await this.$http.post('/sys-job/delLog', jobLogIds)
      console.log(res);
      if (res.code === 20000) {
        console.log(res);
        this.getUserList()
        return this.$message.success('删除成功')
      } else {
        this.$message.error(res.msg)
      }
    },
    handleSelectionChange(selection) { // 多选按钮 拿到数组该项的数据
      this.ids = selection.map(item => item.jobLogId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    // handleSelectionChange (val) { 拿到多选数组每一项
    //   console.log(val);
    //   this.multipleSelection = val;
    // },
    // 关闭 返回到 定时任务页面
    closeViewHandler() {
      this.$router.push('/tasks') 
    }
  },
  /**
  * el 被新创建的 vm.el 替换，并挂载到实例上去之后调用该钩子。
  * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.el 也在文档内。
  */
  mounted() { },
  // 组件方法
}
</script>

<style scoped lang="less">
// 表格样式
.el-table {
  margin-top: 15px;
  font-size: 12px;
}
</style>
